import { createStore } from "vuex";
import vuexPersist from "vuex-persist";
export default createStore({
  state: {
    list: [],
  },
  mutations: {
    add(state, val) {
      state.list.push(val);
    },
    del(state, index) {
      state.list.splice(index, 1);
    },
    //删除

    remove(state, val) {
      // val代表选中的要删的数组
      state.list = state.list.filter((item) => {
        return val.includes(item) == false;
        //把不删的留下来 如果他不在val中就代表他不删要被留下
      });
    },
    // 选中删除
  },
  actions: {},
  modules: {},
  getters: {
    num(state) {
      let numXian = 0;
      let numChang = 0;
      state.list.forEach((item) => {
        if (item.to == "西安") {
          numXian++;
        }
        if (item.to == "昌平") {
          numChang++;
        }
      });

      return { numXian, numChang };
    },
  },
  plugins: [
    new vuexPersist({
      storage: window.localStorage,
    }).plugin,
  ],
});
